{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "14e9ea1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from dpipe.io import load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "804fd1ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MRI</th>\n",
       "      <th>brain_mask</th>\n",
       "      <th>fold</th>\n",
       "      <th>tomograph_model</th>\n",
       "      <th>tesla_value</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>z</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>CC0030</th>\n",
       "      <td>images/CC0030_philips_15_42_F.nii.gz</td>\n",
       "      <td>masks/CC0030_philips_15_42_F_ss.nii.gz</td>\n",
       "      <td>4</td>\n",
       "      <td>philips</td>\n",
       "      <td>15</td>\n",
       "      <td>1.000008</td>\n",
       "      <td>0.888889</td>\n",
       "      <td>0.888889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CC0326</th>\n",
       "      <td>images/CC0326_ge_3_55_M.nii.gz</td>\n",
       "      <td>masks/CC0326_ge_3_55_M_ss.nii.gz</td>\n",
       "      <td>3</td>\n",
       "      <td>ge</td>\n",
       "      <td>3</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CC0187</th>\n",
       "      <td>images/CC0187_siemens_3_63_F.nii.gz</td>\n",
       "      <td>masks/CC0187_siemens_3_63_F_ss.nii.gz</td>\n",
       "      <td>1</td>\n",
       "      <td>siemens</td>\n",
       "      <td>3</td>\n",
       "      <td>1.329996</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CC0114</th>\n",
       "      <td>images/CC0114_philips_3_60_M.nii.gz</td>\n",
       "      <td>masks/CC0114_philips_3_60_M_ss.nii.gz</td>\n",
       "      <td>5</td>\n",
       "      <td>philips</td>\n",
       "      <td>3</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CC0231</th>\n",
       "      <td>images/CC0231_siemens_3_56_M.nii.gz</td>\n",
       "      <td>masks/CC0231_siemens_3_56_M_ss.nii.gz</td>\n",
       "      <td>1</td>\n",
       "      <td>siemens</td>\n",
       "      <td>3</td>\n",
       "      <td>1.330005</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         MRI  \\\n",
       "id                                             \n",
       "CC0030  images/CC0030_philips_15_42_F.nii.gz   \n",
       "CC0326        images/CC0326_ge_3_55_M.nii.gz   \n",
       "CC0187   images/CC0187_siemens_3_63_F.nii.gz   \n",
       "CC0114   images/CC0114_philips_3_60_M.nii.gz   \n",
       "CC0231   images/CC0231_siemens_3_56_M.nii.gz   \n",
       "\n",
       "                                    brain_mask  fold tomograph_model  \\\n",
       "id                                                                     \n",
       "CC0030  masks/CC0030_philips_15_42_F_ss.nii.gz     4         philips   \n",
       "CC0326        masks/CC0326_ge_3_55_M_ss.nii.gz     3              ge   \n",
       "CC0187   masks/CC0187_siemens_3_63_F_ss.nii.gz     1         siemens   \n",
       "CC0114   masks/CC0114_philips_3_60_M_ss.nii.gz     5         philips   \n",
       "CC0231   masks/CC0231_siemens_3_56_M_ss.nii.gz     1         siemens   \n",
       "\n",
       "        tesla_value         x         y         z  \n",
       "id                                                 \n",
       "CC0030           15  1.000008  0.888889  0.888889  \n",
       "CC0326            3  1.000000  1.000000  1.000000  \n",
       "CC0187            3  1.329996  1.000000  1.000000  \n",
       "CC0114            3  1.000000  1.000000  1.000000  \n",
       "CC0231            3  1.330005  1.000000  1.000000  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path_base = Path('/gpfs/data/gpfs0/b.shirokikh/experiments/da/miccai2021_spottune/baseline/cc359_unet2d_one2all/')\n",
    "path_oracle = Path('/gpfs/data/gpfs0/b.shirokikh/experiments/da/miccai2021_spottune/baseline/cc359_unet2d_oracle/')\n",
    "\n",
    "meta = pd.read_csv('/gpfs/data/gpfs0/b.shirokikh/data/cc359/meta.csv', index_col='id')\n",
    "meta.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2816b402",
   "metadata": {},
   "outputs": [],
   "source": [
    "records = []\n",
    "for s in sorted(meta['fold'].unique()):\n",
    "    res_row = {}\n",
    "    \n",
    "    # one2all results:\n",
    "    sdices = load(path_base / f'experiment_{s}/test_metrics/sdice_score.json')\n",
    "    for t in sorted(set(meta['fold'].unique()) - {s}):\n",
    "        df_row = meta[meta['fold'] == t].iloc[0]\n",
    "        target_name = df_row['tomograph_model'] + str(df_row['tesla_value'])\n",
    "        \n",
    "        ids_t = meta[meta['fold'] == t].index\n",
    "        res_row[target_name] = np.mean([sdsc for _id, sdsc in sdices.items() if _id in ids_t])\n",
    "    \n",
    "    df_row = meta[meta['fold'] == s].iloc[0]\n",
    "    source_name = df_row['tomograph_model'] + str(df_row['tesla_value'])\n",
    "    sdices = {}\n",
    "    for n_val in range(3):\n",
    "        sdices = {**sdices,\n",
    "                  **load(path_oracle / f'experiment_{s * 3 + n_val}/test_metrics/sdice_score.json')}\n",
    "    res_row[source_name] = np.mean(list(sdices.values()))\n",
    "\n",
    "    res_row[' '] = source_name\n",
    "    records.append(res_row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "de3693cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame.from_records(records, index=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "beeecfd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>siemens15</th>\n",
       "      <th>siemens3</th>\n",
       "      <th>ge15</th>\n",
       "      <th>ge3</th>\n",
       "      <th>philips15</th>\n",
       "      <th>philips3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>siemens15</th>\n",
       "      <td>0.858110</td>\n",
       "      <td>0.696067</td>\n",
       "      <td>0.364585</td>\n",
       "      <td>0.737310</td>\n",
       "      <td>0.581591</td>\n",
       "      <td>0.546969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>siemens3</th>\n",
       "      <td>0.608170</td>\n",
       "      <td>0.872101</td>\n",
       "      <td>0.107161</td>\n",
       "      <td>0.658075</td>\n",
       "      <td>0.402445</td>\n",
       "      <td>0.371160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ge15</th>\n",
       "      <td>0.700031</td>\n",
       "      <td>0.629186</td>\n",
       "      <td>0.836391</td>\n",
       "      <td>0.580418</td>\n",
       "      <td>0.806356</td>\n",
       "      <td>0.625905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ge3</th>\n",
       "      <td>0.655559</td>\n",
       "      <td>0.731467</td>\n",
       "      <td>0.363212</td>\n",
       "      <td>0.886189</td>\n",
       "      <td>0.517381</td>\n",
       "      <td>0.359648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>philips15</th>\n",
       "      <td>0.689121</td>\n",
       "      <td>0.645036</td>\n",
       "      <td>0.621807</td>\n",
       "      <td>0.589849</td>\n",
       "      <td>0.876841</td>\n",
       "      <td>0.532264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>philips3</th>\n",
       "      <td>0.775209</td>\n",
       "      <td>0.717082</td>\n",
       "      <td>0.551598</td>\n",
       "      <td>0.744775</td>\n",
       "      <td>0.740148</td>\n",
       "      <td>0.855453</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           siemens15  siemens3      ge15       ge3  philips15  philips3\n",
       "                                                                       \n",
       "siemens15   0.858110  0.696067  0.364585  0.737310   0.581591  0.546969\n",
       "siemens3    0.608170  0.872101  0.107161  0.658075   0.402445  0.371160\n",
       "ge15        0.700031  0.629186  0.836391  0.580418   0.806356  0.625905\n",
       "ge3         0.655559  0.731467  0.363212  0.886189   0.517381  0.359648\n",
       "philips15   0.689121  0.645036  0.621807  0.589849   0.876841  0.532264\n",
       "philips3    0.775209  0.717082  0.551598  0.744775   0.740148  0.855453"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23d4112c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
